package com.baturu.vin.hyundai.dal.dao;

import com.baturu.vin.hyundai.dal.entity.HyundaiKiaCombinationEntity;
import com.baturu.vin.hyundai.dto.HyundaiAttributeEntity;
import com.baturu.vin.hyundai.dto.HyundaiKiaBtrRelationEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.baturu.vin.hyundai.constant.HyundaiKiaTable.*;

public interface HyundaiKiaCommonDAO {

    @Insert("<script>" +
            "INSERT INTO `" + T_EPC_CAR_TYPE + "` (`source`, `version`, `bpno`, `combination`) " +
            "SELECT t.source, #{version}, t.bpno, t.combination FROM (" +
            "<foreach item='item' index='index' collection='combinations' open='' separator=' UNION ALL ' close=''>" +
            "select #{source} source, #{bpno} bpno, #{item} combination" +
            "</foreach>" +
            ") t " +
            "LEFT JOIN `" + T_EPC_CAR_TYPE + "` c on c.source = t.source AND c.combination = t.combination AND c.bpno = t.bpno " +
            "WHERE c.id is null" +
            "</script>")
    void saveModelCombination(@Param("source") int source, @Param("version") String version, @Param("bpno") String bpno, @Param("combinations") Collection<String> combinations);

    @Select("<script>" +
            "SELECT `id`, `source`, `bpno`, `combination` FROM `" + T_EPC_CAR_TYPE + "`" +
            "WHERE bpno = #{bpno} AND combination IN " +
            "<foreach item='item' index='index' collection='combinations' open='(' separator=', ' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<HyundaiKiaCombinationEntity> queryModelCombination(@Param("bpno") String bpno, @Param("combinations") Set<String> combinations);

    @Insert("<script>" +
            "INSERT INTO `" + T_MAJOR_ATTRIBUTE + "` (`source`, `version`, `bpno`, `type`, `code`, `describe`, `remark`) " +
            "SELECT t.source, #{version}, t.`bpno`, t.`type`, t.`code`, t.`describe`, t.`remark` FROM (" +
            "<foreach item='item' index='index' collection='attributes' open='' separator=' UNION ' close=''>" +
            "select #{source} `source`, #{bpno} `bpno`, #{item.type} `type`, #{item.code} `code`, #{item.describe} `describe`, #{item.remark} `remark`" +
            "</foreach>" +
            ") t " +
            "LEFT JOIN `" + T_MAJOR_ATTRIBUTE + "` c on c.source = t.source AND c.bpno = t.bpno AND c.type = t.type AND c.code = t.code " +
            "WHERE c.id is null" +
            "</script>")
    void saveMajorAttribute(@Param("source") int source, @Param("version") String version, @Param("bpno") String bpno, @Param("attributes") Collection<HyundaiAttributeEntity> attributes);

    @Select({"<script>",
            "SELECT `bpno`, `type`, `code`, `describe`, `remark` FROM `" + T_MAJOR_ATTRIBUTE + "` ",
            "WHERE source = #{source} AND bpno = #{bpno} AND (`type`, `code`) IN ",
            "<foreach item='item' index='index' collection='majorParameter' open='(' separator=', ' close=')'>",
            "(#{item.type}, #{item.code})",
            "</foreach>",
            "</script>"})
    List<HyundaiAttributeEntity> queryMajorAttributeByCode(@Param("source") int source, @Param("bpno") String bpno, @Param("majorParameter") List<HyundaiAttributeEntity> majorParameter);

    @Insert("<script>" +
            "INSERT INTO `" + T_MINOR_ATTRIBUTE + "` (`version`, `bpno`, `type`, `code`, `describe`) " +
            "SELECT #{version}, t.`bpno`, t.`type`, t.`code`, t.`describe` FROM (" +
            "<foreach item='item' index='index' collection='attributes' open='' separator=' UNION ' close=''>" +
            "select #{bpno} `bpno`, #{item.type} `type`, #{item.code} `code`, #{item.describe} `describe`" +
            "</foreach>" +
            ") t " +
            "LEFT JOIN `" + T_MINOR_ATTRIBUTE + "` c on c.bpno = t.bpno AND c.type = t.type AND c.code = t.code " +
            "WHERE c.id is null" +
            "</script>")
    void saveMinorAttribute(@Param("version") String version, @Param("bpno") String bpno, @Param("attributes") Collection<HyundaiAttributeEntity> attributes);

    @Select({"<script>",
            "SELECT d.`bpno`, d.`startYear`, d.`endYear`, ",
            "m.`brand`, m.`subBrand`, m.`chName` carSystemName, m.`carSystemId` ",
            "FROM `" + T_MODEL_DETAIL + "` d ",
            "INNER JOIN `" + T_CAR_SYSTEM_MAPPING + "` m ON d.`bpno` = m.`bpno` ",
            "WHERE m.`isIgnore` = 0 and m.`bpno` = #{bpno} limit 1 ",
            "</script>"})
    Map<String, Object> queryEpcCarTypeByBpno(@Param("bpno") String bpno);

    @Select({"<script>",
            "select `code` from `hyundai_feature` where `message` = 'ENGINE PACKAGE - VGT REGULAR ENG' ",
            "</script>"})
    Set<String> queryTurboFeatureCodes();
}